package com.wangsd.web.service.impl;

import com.wangsd.core.entity.JSONResult;
import com.wangsd.core.util.ExcelModelStyleUtil;
import com.wangsd.web.dao.RoominfoMapper;
import com.wangsd.web.model.Housinginfo;
import com.wangsd.web.model.Roominfo;
import com.wangsd.web.model.RoominfoExample;
import com.wangsd.web.modelCustom.BillExcelBean;
import com.wangsd.web.modelCustom.ParentCustom;
import com.wangsd.web.modelCustom.RoominfoCustom;
import com.wangsd.web.service.HousinginfoService;
import com.wangsd.web.service.RoominfoService;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RoominfoServiceImpl implements RoominfoService {

    @Autowired
    RoominfoMapper roominfoMapper;
    @Autowired
    HousinginfoService housinginfoService;

    @Override
    public List<RoominfoCustom> queryRoominfoList(RoominfoCustom roominfo) {
        return roominfoMapper.queryRoominfoList(roominfo);
    }

    @Override
    public Roominfo selectRoominfoById(Integer id) {
        return roominfoMapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteRoominfo(int id) {
        Roominfo roominfo = new Roominfo();
        roominfo.setId(id);
        roominfo.setDeletestatus(true);
        roominfo.setStatus(false);
        int ret = roominfoMapper.updateByPrimaryKeySelective(roominfo);
        if (ret > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean insertRoominfo(Roominfo roominfo) {
        int ret = roominfoMapper.insertSelective(roominfo);
        if (ret > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean updateRoominfo(Roominfo roominfo) {
        int ret = roominfoMapper.updateByPrimaryKeySelective(roominfo);
        if (ret > 0) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public List<ParentCustom> queryParentRoomById(Integer housingId) {
        return roominfoMapper.queryParentRoomById(housingId);
    }

    @Override
    public Roominfo selectRoominfoByContent(Roominfo roominfo) {
        RoominfoExample example = new RoominfoExample();
        RoominfoExample.Criteria criteria = example.createCriteria();
        if (roominfo.getParentId() != null && !"".equals(roominfo.getParentId())) {
            criteria.andParentIdEqualTo(roominfo.getParentId());
        }
        if (roominfo.getGroupName() != null && !"".equals(roominfo.getGroupName())) {
            criteria.andGroupNameEqualTo(roominfo.getGroupName());
        }
        if (roominfo.getBuilding() != null && !"".equals(roominfo.getBuilding())) {
            criteria.andBuildingEqualTo(roominfo.getBuilding());
        }
        if (roominfo.getUnit() != null && !"".equals(roominfo.getUnit())) {
            criteria.andUnitEqualTo(roominfo.getUnit());
        }
        if (roominfo.getRoom() != null && !"".equals(roominfo.getRoom())) {
            criteria.andRoomEqualTo(roominfo.getRoom());
        }
        if (roominfo.getAddress() != null && !"".equals(roominfo.getAddress())) {
            criteria.andAddressEqualTo(roominfo.getAddress());
        }
        criteria.andDeletestatusEqualTo(false);
        List<Roominfo> list = roominfoMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        } else {
            return null;
        }
    }

    @Override
    public JSONResult importRoominfo(List<BillExcelBean> list) {
        JSONResult jsonResult = new JSONResult();
        jsonResult.setMessage("开始导入。");
        Housinginfo housinginfo = housinginfoService.selectHousingByName(list.get(0).getHousingName());
        if (housinginfo != null) {
            //处理数据
            int i = 2;
            for (BillExcelBean bean : list) {
                Roominfo roominfo = new Roominfo();
                roominfo.setParentId(housinginfo.getId());
                roominfo.setGroupName(bean.getGroupName());
                roominfo.setBuilding(bean.getBuildName());
                roominfo.setUnit(bean.getUnitName());
                roominfo.setRoom(bean.getRoomName());
                Roominfo old = selectRoominfoByContent(roominfo);
                if (old != null) {
                    jsonResult.setMessage(jsonResult.getMessage() + "第" + i + "行，房屋信息已存在。");
                } else {
                    roominfo.setParentId(housinginfo.getId());
                    roominfo.setOwnerName(bean.getUserName());
                    roominfo.setOwnerPhone(bean.getPhoneName());
                    String groupName = bean.getGroupName();
                    if(groupName!=null&&"null".equals(groupName)){
                        roominfo.setAddress(groupName + bean.getBuildName() + bean.getUnitName() + bean.getRoomName());
                    }else {
                        roominfo.setAddress(bean.getBuildName() + bean.getUnitName() + bean.getRoomName());
                    }
                    roominfo.setCreateTime(new Date());
                    this.insertRoominfo(roominfo);
                }
                i++;
            }
        }else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("小区不存在。");
            return jsonResult;
        }
        jsonResult.setSuccess(true);
        return jsonResult;
    }

    @Override
    public Integer queryRoominfoListCount(RoominfoCustom roominfo) {
        return roominfoMapper.queryRoominfoListCount(roominfo);
    }

	@Override
	public List<ParentCustom> queryBuildingByHousingId(Roominfo roominfo) {
		return roominfoMapper.queryBuildingByHousingId(roominfo);
	}

	@Override
	public List<ParentCustom> queryUnitByBuilding(Roominfo roominfo) {
		return roominfoMapper.queryUnitByBuilding(roominfo);
	}

	@Override
	public List<ParentCustom> queryRoominfoIdByUnit(Roominfo roominfo) {
		return roominfoMapper.queryRoominfoIdByUnit(roominfo);
	}

    @Override
    public HSSFWorkbook exportRoomList(RoominfoCustom roominfo) {
        HSSFWorkbook workbook = new HSSFWorkbook();
        HSSFSheet sheet = workbook.createSheet();
        ExcelModelStyleUtil.setTitleCellStylesNew(workbook, sheet);
        List<RoominfoCustom> list1 = roominfoMapper.queryRoominfoList(roominfo);

        List<String[]> list2 = new ArrayList<String[]>();
        list2.add(new String[]{"序号", "小区名称", "姓名", "手机", "证件号",
                "组团", "楼栋", "单元", "房间号"});
        for (int i = 0; i < list1.size(); i++) {
            String housingName = list1.get(i).getHousingName();
            String ownerName = list1.get(i).getOwnerName();
            String ownerPhone = list1.get(i).getOwnerPhone();
            String ownerCard = list1.get(i).getOwnerCard();
            String groupName = list1.get(i).getGroupName();
            String building = list1.get(i).getBuilding();
            String unit = list1.get(i).getUnit();
            String room = list1.get(i).getRoom();

            list2.add(new String[]{String.valueOf(i + 1), housingName, ownerName, ownerPhone,
                    ownerCard, groupName, building, unit, room});
        }
        for (int i = 0; i < list2.size(); i++) {
            HSSFRow row = sheet.createRow(i);
            String[] cells = list2.get(i);
            for (int j = 0; j < cells.length; j++) {
                String cellValue = cells[j];
                HSSFCell cell = row.createCell(j);
                cell.setCellValue(cellValue);
            }
        }
        return workbook;
    }
}
